<template>
<div class="aui-flexView">
	<div class="aui-scrollView">
		<div class="aui-sign-head">
			<img src="../../assets/login/head.png" alt="">
		</div>
		<div class="aui-sign-form">
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="phone" class="formitemsty">
				<div class="aui-flex">
					<i class="el-icon-mobile-phone iconsty"></i>
					<div class="aui-flex-box">
						<el-input type="text"   class="inputstyle" v-model="ruleForm.phone" autocomplete="off" placeholder="输入手机号码"></el-input>
					</div>
				</div>
        </el-form-item>
        <el-form-item prop="name" class="formitemsty">
                <div class="aui-flex">
					<!-- <i class="icon iconfont">&#xe60e;</i> -->
          <i class="el-icon-user iconsty"></i>
					<div class="aui-flex-box">          
						<el-input type="text"  class="inputstyle" v-model="ruleForm.name" autocomplete="off" placeholder="输入姓名"></el-input>
					</div>
				</div>
        </el-form-item>
        <el-form-item prop="IdCard" class="formitemsty">
                <div class="aui-flex">
					<!-- <i class="icon iconfont">&#xe629;</i> -->
          <i class="el-icon-postcard iconsty"></i>
					<div class="aui-flex-box">
						<el-input type="text"   class="inputstyle" v-model="ruleForm.IdCard" autocomplete="off" placeholder="输入身份证号"></el-input>
					</div>
				</div>
        </el-form-item>
        <el-form-item prop="codes" class="formitemsty">
				<div class="aui-flex">
					<!-- <i class="icon icon-code"></i> -->
          <i class="el-icon-lock iconsty"></i>
					<div class="aui-flex-box">
						<el-input type="password"  class="inputstyle" v-model="ruleForm.codes" autocomplete="new-password" placeholder="输入密码"></el-input>
					</div>
				</div>
        </el-form-item>
        <el-form-item class="formitemsty"> 
				<el-button class="aui-sign-login" @click="submitForm('ruleForm')">注册</el-button>
				<div class="aui-flex aui-flex-clear">
					<div class="aui-flex-box">
                        <div @click="goLogin">
                            已有账号，<span class="resignBtn">去登录</span>
                        </div>
					</div>
		
				</div>
        </el-form-item>
			</el-form>

		</div>

    </div>
</div>
</template>

<script>
export default {
  data() {
    var validatephone = (rule, value, callback) => {
      if (value === "") {
        callback(
          this.$message({
            message: "请输入手机号码",
            type: "warning"
          })
        );
      } else {
        if (!/^1[3456789]\d{9}$/.test(value)) {
          callback(
            this.$message({
              message: "手机号码格式错误，请重新输入！",
              type: "warning"
            })
          );
        } else {
          this.$axios.get("/AppLogin/" + value + "/V1").then(json => {
            if (json.data == "no") {
              callback();
            } else {
              callback(
                this.$message({
                  message: "手机号码已存在，请更换！",
                  type: "warning"
                })
              );
            }
          });
        }
      }
    };
    var validatecard = (rule, value, callback) => {
      if (value === "") {
        callback(
          this.$message({
            message: "请输入身份证号码！",
            type: "warning"
          })
        );
      } else {
        var reg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/;
        if (!reg.test(value)) {
          callback(
            this.$message({
              message: "身份证号码格式错误，请重新输入！",
              type: "warning"
            })
          );
        } else {
          this.$axios.get("/AppLogin/" + value + "/V2").then(json => {
            if (json.data == "no") {
              callback();
            } else {
              callback(
                this.$message({
                  message: "身份证号码已存在，请更换！",
                  type: "warning"
                })
              );
            }
          });
        }
      }
    };
    var validatename = (rule, value, callback) => {
      if (value === "") {
        callback(
          this.$message({
            message: "请填写姓名！",
            type: "warning"
          })
        );
      } else {
        callback();
      }
    };
    var validatepsw = (rule, value, callback) => {
      if (value === "") {
        callback(
          this.$message({
            message: "请填写密码！",
            type: "warning"
          })
        );
      } else if (value.length < 8) {
        callback(
          this.$message({
            message: "密码长度不能小于8位",
            type: "warning"
          })
        );
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: "", //员工姓名
        phone: "", //员工手机号码
        IdCard: "", //员工身份证号码
        codes: "" //密码
      },
      rules: {
        name: [{ required: true, validator: validatename, trigger: "blur" }],
        phone: [{ required: true, validator: validatephone, trigger: "blur" }],
        IdCard: [{ required: true, validator: validatecard, trigger: "blur" }],
        codes: [{ required: true, validator: validatepsw, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var list = {
            St_Name: this.ruleForm.name,
            St_Phone: this.ruleForm.phone,
            St_Card: this.ruleForm.IdCard,
            St_pwd: this.ruleForm.codes
          };
          this.$axios
            .post("/AppLogin/", list)
            .then(Jsondata => {
              if (Jsondata.data != "no") {
                this.$message({
                  type: "success",
                  message: "注册成功！"
                });
                this.$store.commit('logname',Jsondata.data.student.St_Phone); 
                this.$store.commit('logtoken',Jsondata.data.token); // token
                localStorage.setItem(
                  "studentinfo",
                  JSON.stringify(Jsondata.data.student)
                ); //登录人信息
                // localStorage.setItem("logininfo",Jsondata.data.student.St_Phone); //登录人名称
                this.$router.push("appmain");
                // this.$router.push({
                //   path: "/appmain",
                //   query: { dtid: localStorage.getItem("dt_id") }
                // });
              } else {
                this.$message({
                  type: "error",
                  message: "注册失败，请重试或检查登录状态！"
                });
              }
            })
            .catch(error => {});
        } else {
          return false;
        }
      });
    },
    goLogin() {
      this.$router.push({
        path: "/applogin",
        query: { dtid: localStorage.getItem("dt_id") }
      });
    }
  }
};
</script>

<style scoped>
@font-face {
  font-family: "iconfont"; /* project id 1506859 */
  src: url("//at.alicdn.com/t/font_1506859_ac789nivaaj.eot");
  src: url("//at.alicdn.com/t/font_1506859_ac789nivaaj.eot?#iefix")
      format("embedded-opentype"),
    url("//at.alicdn.com/t/font_1506859_ac789nivaaj.woff2") format("woff2"),
    url("//at.alicdn.com/t/font_1506859_ac789nivaaj.woff") format("woff"),
    url("//at.alicdn.com/t/font_1506859_ac789nivaaj.ttf") format("truetype"),
    url("//at.alicdn.com/t/font_1506859_ac789nivaaj.svg#iconfont") format("svg");
}
.iconfont {
  font-family: "iconfont" !important;
  font-size: 20px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

html,
body {
  color: #333;
  margin: 0;
  height: 100%;
  font-family: "Myriad Set Pro", "Helvetica Neue", Helvetica, Arial, Verdana,
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-weight: normal;
}

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

a {
  text-decoration: none;
  color: #000;
}

a,
label,
button,
input,
select {
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

img {
  border: 0;
}

body {
  background: #ededed;
  color: #666;
}

html,
body,
div,
dl,
dt,
dd,
ol,
ul,
li,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
button,
fieldset,
form,
input,
legend,
textarea,
th,
td {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color: #08acee;
}

button {
  outline: 0;
}

img {
  border: 0;
}

button,
input,
optgroup,
select,
textarea {
  margin: 0;
  font: inherit;
  color: inherit;
  outline: none;
}

li {
  list-style: none;
}

a {
  color: #666;
}

.clearfix::after {
  clear: both;
  content: ".";
  display: block;
  height: 0;
  visibility: hidden;
}

.aui-flex {
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  align-items: center;
  padding: 15px;
  position: relative;
}

.aui-flex-box {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  flex: 1;
  min-width: 0;
  font-size: 14px;
  color: #333;
  text-align: center;
}
.formitemsty {
  line-height: 2.5rem !important;
}
.iconsty {
  font-size: 1.1rem;
  color: #a0a0a0;
  padding: 0.1rem;
  height: 1.1rem;
}

.aui-sign-form .formitemsty /deep/ .el-form-item__content {
  line-height: 1.5rem !important;
}
.aui-sign-form .inputstyle /deep/ .el-input__inner {
  border: none;
  height: 30px;
  padding: 0 8px;
}
.aui-sign-form .formitemsty /deep/ .el-form-item__error {
  display: none;
}
/* 必要布局样式css */

.aui-flexView {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
}

.aui-scrollView {
  width: 100%;
  height: 100%;
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  -webkit-overflow-scrolling: touch;
  position: relative;
  margin-top: -44px;
}
.icon {
  width: 20px;
  height: 20px;
  display: block;
  border: none;
  float: left;
  background-size: 20px;
  background-repeat: no-repeat;
}

.icon-return {
  background-image: url("");
}
.aui-sign-head {
  width: 100%;
  position: relative;
  height: auto;
}

.aui-sign-head img {
  width: 100%;
  height: auto;
  display: block;
  border: none;
}

.aui-sign-form {
  width: 92%;
  background-color: white;
  box-shadow: 0 3px 9px #cfcfcf;
  border-radius: 5px;
  margin: -6rem auto;
  z-index: 1;
  position: relative;
  padding: 1rem 1rem;
}

.aui-sign-form .aui-flex {
  border: 1px solid #cccccc;
  border-radius: 2px;
  margin: 0.7rem 0;
  padding: 0.2rem 0 0.2rem 0.3rem;
}
.aui-sign-form .aui-flex input {
  width: 100%;
  height: 2rem;
  line-height: 2rem;
  background: none;
  border: none;
  font-size: 0.89rem;
}
.aui-sign-login {
  width: 100%;
  color: #fff;
  font-size: 0.98rem;
  border-radius: 22px;
  background-color: #5f8cd0;
  border: none;
  height: 2.5rem;
  line-height: 2.5rem;
  margin-top: 0.5rem;
}

.aui-code {
  padding-right: 1rem;
}

.aui-sign-form .aui-code input {
  color: #999999;
  font-size: 0.85rem;
}

.icon-phone {
  background-image: url("");
}

.icon-code {
  background-image: url("");
}

.aui-sign-form .aui-flex-clear {
  border: none;
}

.aui-sign-form .aui-flex-clear a {
  color: #999999;
  font-size: 0.85rem;
}
.resignBtn,
.findPwdBtn {
  color: red;
}
</style>
